﻿using System.Collections;
using System.Collections.Generic;
using Gp.Scripts.Core.Unit;
using Gp.Scripts.Data;
using Sirenix.OdinInspector;


namespace Gp.AI {
    [CreateNodeMenu("一般行为/切换武器")]
    public class ChangeWeaponAINode : NormalAINode {
        public const string PortName = "weapon";
        [LabelText("目标武器")] public ConfigWeapon targetWeapon;


        public override AINodeEntry NodeEntry() {
            return new ChangeWeaponAINodeEntry(this);
        }
    }


    public class ChangeWeaponAINodeEntry : NormalAINodeEntry<ChangeWeaponAINode> {
        public ChangeWeaponAINodeEntry(ChangeWeaponAINode data) : base(data) { }

        // public WeaponEntity SelectedWeapon() {
        //     
        // }

        private int _index = -1;

        protected override void PreCalculate() {
            var arr = Unit.ComEquip.Weapons;
            for (int i = 0; i < arr.Length; i++) {
                if (arr[i].Config != Data.targetWeapon) continue;
                
                _index = i;
                break;
            }
        }

        public override bool ShouldPerform() {
            return _index != -1 && _index != Unit.ComEquip.CurWeaponIndex;
        }

        public override IEnumerator OnExecute() {
            Unit.ComEquip.ChangeCurWeapon(_index);
            yield break;
        }
        
        
        // private int SelectWeapon(BaseUnit target, WeaponAttackEntity atkSkill) {
        //     int max = 0;
        //     int res = -1;
        //     var arr = Unit.EquipHolder.Weapons;
        //     
        //     for (int i = 0; i < arr.Length; i++) {
        //         // 如果有武器输入节点，则优先使用
        //         if (!atkSkill.AvailableTargets(i).Contains(target.CurrentCell)) continue;
        //         if (!arr[i].Info.CanAttack()) continue;
        //         var damage = arr[i].GetDamage();
        //         
        //         if (damage <= max) continue;
        //         
        //         max = damage;
        //         res = i;
        //     }
        //
        //     return res;
        // }


        protected override AIActionArgs GetActionInfo() {
            return new AIActionArgs("切换武器", $"切换武器为 {Unit.ComEquip.Weapons[_index].Config.Name}", null, Unit);
        }
    }
}